package com.example.wheresmystuff2;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class EditUser extends Activity {
	Database db;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_user);
		
		db = new Database(this);
		
		//Visuals on the activity
		final EditText userInput= (EditText) findViewById(R.id.edit_user_username);
		final RadioGroup lockedStatus= (RadioGroup) findViewById(R.id.edit_user_lock_status_radio_group);
		final RadioGroup adminStatus= (RadioGroup) findViewById(R.id.edit_user_admin_radio_group);
		final TextView errorField= (TextView) findViewById(R.id.edit_user_errorField);
		Button cancelButton = (Button) findViewById(R.id.edit_user_cancel);
		Button doneButton= (Button) findViewById(R.id.edit_user_done);
		Button deleteButton= (Button) findViewById(R.id.edit_user_delete_user);
		
		//OnClickListener for the done button
		doneButton.setOnClickListener(new OnClickListener()
		{
			/**
			 * This is an onClick method that edits the user based on input given
			 */
			public void onClick(View v) {
				db.open();
				String username=userInput.getText().toString();
				final RadioButton locked= (RadioButton) findViewById(lockedStatus.getCheckedRadioButtonId());
				int isLocked;
				if	(locked.getText().toString().equals("Locked"))	{
					isLocked=1;
				}
				else	{
					isLocked=0;
				}
				final RadioButton admin= (RadioButton) findViewById(adminStatus.getCheckedRadioButtonId());
				int isAdmin;
				if (admin.getText().toString().equals("True")){
					isAdmin = 1;
				}
				else{
					isAdmin = 0;
				}
					
				try{
					ArrayList<Object> list = db.getUserRowAsArray(username);

					list.set(4, isLocked);
					list.set(5, isAdmin);
					db.updateUserRow((String) list.get(0), (String) list.get(1), (String) list.get(2), 3, (Integer) list.get(4), (Integer) list.get(5));
					
					errorField.setText("User successfully updated.");
					
				}
				catch(Exception e){
					errorField.setText("User does not exist.");
				}
				db.close();			
			}
		});
		
		//OnClickListener for the cancel button
		cancelButton.setOnClickListener(new OnClickListener()
	    {
			/**
			 * This onClick method brings the user back to SuccessfulSignIn, as you cancelled the editting of user
			 */
			public void onClick(View v){
				Intent myIntent = new Intent(EditUser.this, SuccessfulSignIn.class);
				EditUser.this.startActivity(myIntent);
			}
	    });
		
		//OnClickListener for the delete button
		deleteButton.setOnClickListener(new OnClickListener()
		{
			 /**
			  * This onClick method deletes the user that is specified in the inputUser
			  */
			public void onClick(View v){
				db.open();	
				String username=userInput.getText().toString();
				try{
					db.deleteUserRow(username);
					errorField.setText("User successfully deleted.");
				}
				catch(Exception e){
					errorField.setText("User does not exist.");
				}
				db.close();
			}
	    });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present
		getMenuInflater().inflate(R.menu.edit_user, menu);
		return true;
	}

}
